#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
# 

"""
File: Josephus.py
Author: zhangyang(zhangyang40@baidu.com)
Date: 2017/11/29 15:54
"""
from LCList import LCList


def JosephusA(n, k, m):
    people = list(range(0, n))
    i = k - 1
    for num in range(n, 0, -1):
        count = 0
        while count < m:
            if people[i] >= 0:
                count += 1
            if count == m:
                print people[i]
                people[i] = 0
            i = (i + 1) % n
    return


def JosephusL(n, k, m):
    people = list(range(0, n))
    if k < 1 or k > n:
        raise ValueError

    num, i = n, k - 1
    for num in range(n, 0, -1):
        # print "(%d + %d - 1) mod %d" % (i, m, num)
        i = (i + m - 1) % num
        # print i
        print(people.pop(i))
    return


class Josephus(LCList):
    def turn(self, m):
        for i in range(m):
            self.rear = self.rear.next

    def __init__(self, n, k, m):
        LCList.__init__(self)
        for i in range(n):
            self.append(i + 1)
        self.turn(k - 1)
        while not self.isEmpty():
            self.turn(m - 1)
            print(self.pop())
            if not self.isEmpty():
                print(", ")
            else:
                print("")


# end class Josephus

if __name__ == '__main__':
    # s = input("Josephus parameters (n k m): ")
    # n, k, m = map(int, s.split())
    # JosephusA(n, k, m)
    # JosephusL(n, k, m)
    # Josephus(n, k, m)
    JosephusA(7, 1, 4)
    # JosephusL(7, 1, 4)
